<?php

namespace app\home\controller;

use app\common\model\User;
use think\Controller;
use think\Request;

class UserController extends Controller
{
    //显示用户注册时的验证码
    public function checkNum()
    {
        return captcha_img();
    }
    //处理用户注册
    public function userReg(Request $request)
    {
        $input = $request->only(['user','email','phone','pwd','gender','check']);
        //验证入库
        $re = (new User())->createUser($input);
        return reJson($re);
    }

    //获取单条用户数据
    public function getUser(Int $id)
    {
        $re = User::findOrFail($id);
        return reJson($re);
    }

    //修改用户数据
    public function updateUser(Int $id,Request $request)
    {
        $input = $request->only(['user','phone']);
        $re = (new User())->editUser($input,$id);
        return reJson($re);
    }

    //修改密码
    // 查询原密码是否正确
    public function checkOldPwd(Int $id,Request $request)
    {
        $input = $request->only(['oldpwd']);

    }

    //上传头像
    public function picUpload(Int $id,Request $request)
    {
        $oldfile = $request->only('oldfile') ?? '';
        $re = imgUpload($request,$oldfile['oldfile']);
        if($re['sta']){
            //成功，写入数据库
            $re = (new User())->editOne(['pic'=>$re['data']],$id);
            return reJson($re);
        }else{
            return reJson($re['data']);
        }
    }
}
